/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONObject;
import com.sigmatrix.h5.dao.DisRebateActivityDao;
import com.sigmatrix.h5.dao.DisRebateActivityDetailDao;
import com.sigmatrix.h5.dao.DisRebateConditionDao;
import com.sigmatrix.h5.dao.DisRebateStrategyDao;
import com.sigmatrix.h5.entity.DisRebateActivity;
import com.sigmatrix.h5.enums.ScanType;
import com.sigmatrix.h5.utils.Constants;
import com.sigmatrix.h5.utils.SimpleRowMapper;
import com.sigmatrix.h5.web.mode.RebateActivityInfo;
import com.sigmatrix.sc.jdbc.common.dao.DaoImpl;

/**
 *@ClassName: DisRebateActivityDaoImpl
 *@Description: 
 *@author MH
 *@date 2017年8月15日 下午5:26:36
 */
@Repository
public class DisRebateActivityDaoImpl extends DaoImpl<DisRebateActivity,Integer> implements DisRebateActivityDao{

	@Autowired
	private DisRebateActivityDetailDao disRebateActivityDetailDao;
	
	@Autowired
	private DisRebateStrategyDao disRebateStrategyDao;
	
	@Autowired
	private DisRebateConditionDao disRebateConditionDao;
	
	@Override
	public List<RebateActivityInfo> determineActivityForHierarchyRebate(Integer dealerId, Integer productId) {
		return findByScanType(dealerId, productId, ScanType.HIERARCHY);
	}
	@Override
	public List<RebateActivityInfo> determineActivityForTerminalRebate(Integer dealerId, Integer productId) {
		return findByScanType(dealerId, productId, ScanType.TERMINAL);
	}

	@Override
	public List<RebateActivityInfo> findByScanType(Integer dealerId, Integer productId, ScanType scanType) {
		StringBuffer sql = new StringBuffer();
		sql.append("select a._id,a._strategy_id,c._critical_value _condition_value,c._trig_type _condition_trig_type, ");
		sql.append(" b._condition_id,c._type _condition_type,a._start_time,a._end_time");
		sql.append(" from ");
		sql.append(tableName()).append(" a inner join ");
		sql.append(disRebateStrategyDao.tableName()).append(" b on a._strategy_id = b._id left join ");
		sql.append(disRebateConditionDao.tableName()).append(" c on  b._condition_id = c._id ");
		sql.append(" and c._type in( ");
		for (int i = 0; i <scanType.conditionType.size(); i++) {
			if(i != 0) sql.append(",");
			sql.append(scanType.conditionType.get(i));
		}
		sql.append(")");
		sql.append("where 1=1 ");
		sql.append("and a._start_time <= ? and a._end_time >= ? ");
		sql.append("and a._status =").append(Constants.STATUS_NO_START);
		sql.append(" and exists(");
		
		sql.append("select 1 from ").append(disRebateActivityDetailDao.tableName()).append(" d");
		sql.append(" where a._id = d._rebate_activity_id and d._dealer_id = ").append(dealerId)
		.append(" and d._product_id = ").append(productId).append(" and d._status = ").append(Constants.STATUS_ENABLE);
		sql.append(" union all ");
		sql.append("select 1 from ").append(disRebateActivityDetailDao.tableName()).append(" d");
		sql.append(" where a._id = d._rebate_activity_id and d._dealer_id = ").append(dealerId)
		.append(" and d._is_all_products = ").append(Constants.STATUS_ENABLE).append(" and d._status = ").append(Constants.STATUS_ENABLE);

		sql.append(" union all ");
		sql.append("select 1 from ").append(disRebateActivityDetailDao.tableName()).append(" d");
		sql.append(" where a._id = d._rebate_activity_id and d._product_id = ").append(productId)
		.append(" and d._is_all_dealers = ").append(Constants.STATUS_ENABLE).append(" and d._status = ").append(Constants.STATUS_ENABLE);
		sql.append(" union all ");
		sql.append("select 1 from ").append(disRebateActivityDetailDao.tableName()).append(" d");
		sql.append(" where a._id = d._rebate_activity_id and d._is_all_products = ").append(Constants.STATUS_ENABLE)
		.append(" and d._is_all_dealers = ").append(Constants.STATUS_ENABLE).append(" and d._status = ").append(Constants.STATUS_ENABLE);

		sql.append(")and b._scan_code_type = ? ");
		
		sql.append(" order by a._weight desc,a._create_time desc");
		Date now = new Date();
		List<Object> list = new ArrayList<Object>();
		list.add(now);
		list.add(now);
		list.add(scanType.value);
		logger.info("sql="+sql.toString());
		logger.info("param="+JSONObject.toJSONString(list));
		return jdbcTemplate.query(sql.toString(), SimpleRowMapper.createDefault(RebateActivityInfo.class), list.toArray());
	}

}
